package alexandria.dashboard

import alexandria.dashboard.entity.Project

class PanelController {
    def connectorFactoryService
	def dataService
	def chartDataSets = []
    def charts_default = [[
            position: 1,
            type: "bar",
            name: "task_vs_project",
            title: "Task vs Project"
        ],[
            position: 2,
            type:"pie",
            name: "task_vs_status",
            title: "Task vs Status"
        ],[
            position: 3,
            type: "bar",
            name: "stories_efforts",
            title: "Stories Efforts"
        ],[
            position: 4,
            type: "table",
            name: "metrics",    
            title: "Metrics"
        ]]

    def beforeInterceptor = {
        if (!session.config) {
            redirect (controller:"config")
        }
    }

    def index = {
        if (!session.charts) {
            session.charts = charts_default
        }
        if (session.config) {
		    home()
		}
    }

    def home = {
		if(!session?.project){
			println("session.config = ${session.config}")
            def con = connectorFactoryService.getConnector(session.config)
            session.project = con.doProject(session.config.externalId.toString())
        }
		session.charts.each{
			def chartDataSet
			chartDataSet = dataService.chartDataSet(it, session.project)
			chartDataSets.add(chartDataSet)
		}
		render(view:"main", collection: chartDataSets)
    }

    def update = {
        session.project=null
        home()
    }

}
